package com.woniu.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.entity.Account;
import com.woniu.service.AccountService;
import com.woniu.util.JsonResult;
import com.woniu.util.JwtUtil;
import com.woniu.vo.AccountVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * @program:
 * @ClassName:
 * @description:
 * @author: zgc
 * @date:
 * @Version 1.0
 **/
@RestController
@Slf4j
@RequestMapping("/account")  // 匹配 /account 路径下所有请求
public class AccountController {
    @Resource
    private AccountService accountService;

    @Resource
    JwtUtil jwtUtil;
    @Resource
    HashMap<String,String> map ;
    
    //用户通过验证码登录
    @RequestMapping("/loginByCode")
    public JsonResult<Account> loginByCode(String accountName,String password,String code,String codeId, HttpServletResponse response) {
        log.info("用户名："+accountName+",密码码："+password+",验证码："+code +"----------"+codeId);
        JsonResult<Account> jsonResult = new JsonResult<>();
        // 从缓存中得到codeId匹配的code进行验证
        String mapCode = map.get(codeId);
        if (mapCode != null && mapCode.equalsIgnoreCase(code)) {
            Account accountLogin = accountService.accountLoginByAccountName(accountName);
            if (accountLogin != null &&( accountLogin.getAccountType().equals("01") || accountLogin.getAccountType().equals("00"))) {
              if(accountLogin.getIsDelete() != 1){
                  if (accountLogin.getPassword().equals(password)) {
                      jsonResult.setCode(200);
                      jsonResult.setMessage("登录成功!");
                      accountService.upadateAccountLoginDate(accountLogin);
                      // 登录成功，生成token
                      String token = jwtUtil.createToken(accountName);
                      log.info("后端生成的token:" + token);
                      // 把token存到响应头里面
                      response.setHeader("token", token);
                  }else {
                      jsonResult.setCode(500);
                      jsonResult.setMessage("对不起,密码有误！");
                  }
              }else {
                  jsonResult.setCode(500);
                  jsonResult.setMessage("对不起,该用户被冻结,请联系管理员");
              }
                
            }else {
                jsonResult.setCode(500);
                jsonResult.setMessage("对不起,商户或管理员不存在！");
            }
        }else {
            jsonResult.setCode(500);
            jsonResult.setMessage("对不起,验证码有误！");
        }
        return  jsonResult;
    }
    
    //商户注册
    @RequestMapping("/accountRegisterByMerchant")
    public JsonResult<Integer> accountRegisterByMerchant(String accountName,String password,String code,String codeId) {
        JsonResult<Integer> jsonResult = new JsonResult<>();
        String mapCode = map.get(codeId);
        if (mapCode != null && mapCode.equalsIgnoreCase(code)) {
            Account account = new Account(accountName,password);
            account.setAccountType("01");
            int i = accountService.accountRegisterByMerchant(account);
            return JsonResult.build(200,"商户加盟成功",i);
        }else {
            jsonResult.setCode(500);
            jsonResult.setMessage("对不起,验证码有误！");
        }
        return  jsonResult;
    }
    
    //查看用户信息
    @RequestMapping("/selectAccountByMerchant")
    public JsonResult<PageInfo<AccountVo>> selectAccountByMerchant
    (Integer pageNum, Integer pageSize,String accountName,Long startTime,Long endTime,Integer isDelete) {
        
        PageInfo<AccountVo> list =  accountService.selectAccountByMerchant
                (pageNum,pageSize,accountName,startTime,endTime,isDelete);
        return JsonResult.ok(200,list);
    }
    
    //修改用户状态
    @RequestMapping("/updateAccountStatusByAccountId")
    public JsonResult<Integer> updateAccountStatusByAccountId(Integer accountId,Byte isDelete,String remark){
        Account account = new Account(accountId,isDelete,remark);
        int i = accountService.updateAccountStatusByAccountId(account);
        return JsonResult.build(200,"修改成功",i);
    }
    
    //根据用户账号查找信息
    @RequestMapping("/selectAccountByAccountName")
    public JsonResult<Account> selectAccountByAccountName(String accountName) {
        Account account = accountService.selectAccountByAccountName(new Account(accountName));
        return JsonResult.ok(200,account);
    }
    
    //更新用户信息，根据用户编号
    @RequestMapping("/updateAccountByAccountId")
    public JsonResult<Integer> updateAccountByAccountId(@RequestBody Account account) {
        int i = accountService.updateAccountByAccountId(account);
        return JsonResult.build(200,"修改成功",i);
    }
    
    //按用户消费金额进行排名
    @RequestMapping("/selectAccountByTotal")
    public JsonResult<PageInfo<AccountVo>> selectAccountByTotal(Integer pageNum, Integer pageSize) {
        PageInfo<AccountVo> list =  accountService.selectAccountByTotal(pageNum,pageSize);
        return JsonResult.ok(200,list);
    }
    
    //重名验证
    @RequestMapping("/isSameAccount")
    public JsonResult<Account> isSameAccount(String accountName) {
        JsonResult<Account> jsonResult = new JsonResult<>();
        Account account = accountService.selectAccountByAccountName(new Account(accountName));
        if (account == null) {
            jsonResult.setCode(200);
            jsonResult.setMessage("此用户名未被注册");
        }else {
            jsonResult.setCode(500);
            jsonResult.setMessage("此用户名已被注册");
        }
        return jsonResult;
    }
    
}
